package asteroids.entity.weapon;

import asteroids.engine.GameState;
import asteroids.engine.Utils;
import asteroids.entity.PlayerShip;
import asteroids.entity.Ship;

public class ConfigurableWeapon extends Weapon
{
    private final double BULLET_SPEED;
    private final double BULLET_POWER;
    // private final double ENERGY_COST;
    private final int BULLET_TTL;
    // private final int REFIRE_DELAY;
    private final boolean ROTARY;
    private Utils.PolarCoordinate[] BULLET_SHAPE;
    private int currentHardPoint = 0;

    public ConfigurableWeapon(Ship ship, double bulletSpeed, double bulletPower, double energyCost,
            int timeToLive, int refireDelay, boolean rotateHardpoints,
            Utils.PolarCoordinate[] bulletShape)
    {
        super(ship, energyCost, refireDelay);
        BULLET_SPEED = bulletSpeed;
        BULLET_POWER = bulletPower;
        // ENERGY_COST = energyCost;
        BULLET_TTL = timeToLive;
        // REFIRE_DELAY = refireDelay;
        ROTARY = rotateHardpoints;
        BULLET_SHAPE = bulletShape;
    }

    public ConfigurableWeapon(Ship ship, double bulletSpeed, double bulletPower, double energyCost,
            int timeToLive, int refireDelay, boolean rotateHardpoints)
    {
        this(ship, bulletSpeed, bulletPower, energyCost, timeToLive, refireDelay, rotateHardpoints,
                Utils.POINT_SHAPE);
    }

    @Override
    public boolean FIRE(GameState state)
    {
        boolean fired = false;
        if( canFireYet(state) )
        {
            manageFireControl(state);
            fired = true;

            Utils.PolarCoordinate[] hardPoints = ((PlayerShip) ship).getHardPoints();

            if( ROTARY ) // then only fire from one hardpoint at a time
            {
                Utils.CartesianCoordinate firePoint = Utils.cartesianFromPolar(
                        hardPoints[currentHardPoint].rotateBy(ship.getRotation())).shiftBy(
                        ship.getXPosition(), ship.getYPosition());
                Bullet bill = new Bullet(firePoint.getXComponent(), firePoint.getYComponent(),
                        ship.getRotation(), BULLET_SPEED, BULLET_POWER, BULLET_TTL, BULLET_SHAPE,
                        ship);
                ship.add(bill);
                currentHardPoint = (currentHardPoint + 1) % hardPoints.length;
            }
            else
            // fire from all hardpoints!
            {
                for( Utils.PolarCoordinate currentFirePoint : hardPoints )
                {
                    Utils.CartesianCoordinate firePoint = Utils.cartesianFromPolar(
                            currentFirePoint.rotateBy(ship.getRotation())).shiftBy(
                            ship.getXPosition(), ship.getYPosition());
                    Bullet bill = new Bullet(firePoint.getXComponent(), firePoint.getYComponent(),
                            ship.getRotation(), BULLET_SPEED, BULLET_POWER, BULLET_TTL,
                            BULLET_SHAPE, ship);
                    ship.add(bill);
                }
            }
        }
        return fired;
    }
}
